package com.eastfair.pay.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.pay.dao.ServiceContractMapper;
import com.eastfair.pay.dto.ServiceContractPageQuery;
import com.eastfair.pay.entity.ServiceContract;
import com.eastfair.pay.service.ServiceContractService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.pay.vo.ServiceContractVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 
 * </p>
 *
 * @author clm
 * @date 2022-07-21
 */
@Slf4j
@Service
public class ServiceContractServiceImpl extends SuperServiceImpl<ServiceContractMapper, ServiceContract> implements ServiceContractService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ServiceContract> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 处理新增相关处理
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(ServiceContract model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        return R.successDef();
    }

    @Override
    public Page<ServiceContractVO> queryPage(PageParams<ServiceContractPageQuery> params) {

        ServiceContractPageQuery model = params.getModel();
        LambdaQueryWrapper<ServiceContract> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ServiceContract::getIsDeleted, 0);
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(model.getContractNo()),ServiceContract::getContractNo,model.getContractNo());
        lambdaQueryWrapper.eq(model.getCustomerId() != null, ServiceContract::getCustomerId, model.getCustomerId());
        lambdaQueryWrapper.like(!StrUtil.isBlank(model.getContractName()), ServiceContract::getContractName, model.getContractName());
        IPage<ServiceContract> serviceContractIPage = params.buildPage();
        IPage<ServiceContract> page = page(serviceContractIPage,lambdaQueryWrapper);
        Page ServiceContractPage = ConvertUtil.convertPage((Page) page, ServiceContractVO.class);
        return ServiceContractPage;
    }

    @Override
    public ServiceContractVO getContractById(Long id) {
        List<ServiceContract> serviceContractList = Lists.newArrayList();
        //查询展厅列表
        QueryWrapper<ServiceContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ServiceContract::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(Objects.nonNull(id),ServiceContract::getId,id);
        serviceContractList= this.list(queryWrapper);
        List<ServiceContractVO> contractVOList = ConvertUtil.convertList(serviceContractList, ServiceContractVO.class);
        return contractVOList.get(0);
    }
}
